package cn.jinjimi.app.main.extend;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cn.jinjimi.app.R;
import cn.jinjimi.app.controller.event.LoginSuccessEvent;
import cn.jinjimi.app.main.uicontroller.NavTitleBarController;
import cn.jinjimi.app.util.PreventContinuousClickUtil;
import cn.jinjimi.base.util.LogUtil;

/**
 * @author zhenhua
 */
public abstract class BaseFragment extends Fragment implements View.OnClickListener {
    protected View view;
    protected NavTitleBarController controller;

    public String getTitle() {
        return "";
    }

    @Override
    public void onAttach(Context context) {
        LogUtil.d("BaseFragment", " onAttach()");
        super.onAttach(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        LogUtil.d("BaseFragment", "onCreate()");
        super.onCreate(savedInstanceState);
        registerEventBus();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LogUtil.d("BaseFragment", "onCreateView()");
        if (view != null) {
            ViewGroup p = (ViewGroup) view.getParent();

            if (p != null) {
                p.removeView(view);
            }
        } else {
            view = inflater.inflate(getLayoutResourceId(), container, false);
            controller = new NavTitleBarController(view.findViewById(R.id.nav_bar));
            initial();
        }
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        LogUtil.d("BaseFragment", "onViewCreated()");
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        LogUtil.d("BaseFragment", "onActivityCreated()");
        super.onActivityCreated(savedInstanceState);
    }

    /**
     *  加载layout xml
     *  @return int resid
     */
    protected abstract int getLayoutResourceId();

    /**
     *  初始化数据
     */
    protected abstract void initial();

    @Override
    public void onStart() {
        LogUtil.d("BaseFragment", "onStart()");
        super.onStart();
    }

    @Override
    public void onResume() {
        LogUtil.d("BaseFragment", "onResume()");
        super.onResume();
    }

    @Override
    public void onPause() {
        LogUtil.d("BaseFragment", "onPause()");
        super.onPause();
    }

    @Override
    public void onStop() {
        LogUtil.d("BaseFragment", "onStop()");
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        LogUtil.d("BaseFragment", "onDestroyView()");
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        LogUtil.d("BaseFragment", "onDestroy()");
        unregisterEvenBus();
        if (controller != null) {
            controller.onDestory();
        }
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        LogUtil.d("BaseFragment", "onDetach()");
        super.onDetach();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        LogUtil.d("BaseFragment" + " UserVisibleHint: %s", isVisibleToUser + "");
        if (getFragmentManager() == null) {
            return;
        }

        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void startActivity(Intent intent) {
        LogUtil.d("BaseFragment", "startActivity()");
        super.startActivity(intent);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        LogUtil.d("BaseFragment", "startActivityForResult()");
        super.startActivityForResult(intent, requestCode);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        LogUtil.d("BaseFragment");
        super.onActivityResult(requestCode, resultCode, data);
    }

    public boolean checkActivityExit() {
        return (getActivity() == null || getActivity().isFinishing());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLoginSuccessEvent(LoginSuccessEvent event) {

    }

    @Override
    public void onClick(View v) {
        if (PreventContinuousClickUtil.isContinuousClick()) {
            return;
        }
    }

    public View findView() {
        if (null != view) {
            return view;
        }
        return null;
    }


    public void registerEventBus() {
        EventBus.getDefault().register(this);
    }

    public void unregisterEvenBus() {
        EventBus.getDefault().unregister(this);
    }
}
